﻿var data = [];

var department;
var post;
var role;
var team;
var employee;
var already;

jQuery(function ()
{
    if (querysHelper.object("data"))
    {
        data = querysHelper.object("data");
    }

    $(".tab").find("a").click(function ()
    {
        $(".tab").find("a").removeClass("selected");
        $(this).addClass("selected");

        $(".northwest").find(".list").hide();
        $(".northwest").find("#" + $(this).attr("to")).show();
    });

    department = $("#department").kendoTreeView(
    {
        dataSource:
        {
            transport: { read: "/HR/Department/Read?ResultType=Json&Top=10000" },
            schema:
            {
                data: "Data",
                model: { id: "id", text: "text", children: "childs" },
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        dataBound: function (e)
        {
            $("#department").find(".k-item").dblclick(function (e)
            {
                e.stopPropagation();
                var node = department.select().data();
                var item = department.dataSource.getByUid(node.uid);

                var newItem = already.dataSource.get(item.id);
                if (newItem) return;
                newItem = { id: item.id, text: item.text, type: "Department" };
                already.dataSource.data().push(newItem);
            });
        },
        select: function (e)
        {
            var item = this.dataSource.getByUid($(e.node).attr("data-uid"));
            employee.dataSource.read({ DepartmentId: item.id });
        }
    }).data("kendoTreeView");

    post = $("#post").kendoListView(
    {
        dataSource:
        {
            transport: { read: { url: "/HR/Post/Read?ResultType=Json&Top=10000"} },
            schema:
            {
                data: "Data",
                model: { id: "Id" },
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        dataBound: function (e)
        {
            $("#post").find("li").dblclick(function (e)
            {
                e.stopPropagation();
                var node = post.select().data();
                var item = post.dataSource.getByUid(node.uid);

                var newItem = already.dataSource.get(item.id);
                if (newItem) return;
                newItem = { id: item.Id, text: item.Name, type: "Post" };
                already.dataSource.data().push(newItem);
            });
        },
        selectable: "single",
        template: "<li>${Name}</li>",
        change: function (e)
        {
            var node = post.select().data();
            var item = post.dataSource.getByUid(node.uid);
            employee.dataSource.read({ PostId: item.id });
        }
    }).data("kendoListView");

    role = $("#role").kendoListView(
    {
        dataSource:
        {
            transport: { read: { url: "/Common/Role/Read?ResultType=Json&Top=10000&Application=HR"} },
            schema:
            {
                data: "Data",
                model: { id: "Id" },
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        selectable: "single",
        template: "<li>${Name}</li>",
        dataBound: function (e)
        {
            $("#role").find("li").dblclick(function (e)
            {
                e.stopPropagation();
                var node = role.select().data();
                var item = role.dataSource.getByUid(node.uid);

                var newItem = already.dataSource.get(item.id);
                if (newItem) return;
                newItem = { id: item.Id, text: item.Name, type: "Role" };
                already.dataSource.data().push(newItem);
            });
        },
        change: function (e)
        {
            var node = role.select().data();
            var item = role.dataSource.getByUid(node.uid);
            employee.dataSource.read({ RoleId: item.id });
        }
    }).data("kendoListView");

    team = $("#team").kendoListView(
    {
        dataSource:
        {
            transport: { read: { url: "/HR/Team/Read?ResultType=Json&Top=10000"} },
            schema:
            {
                data: "Data",
                model: { id: "Id" },
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        selectable: "single",
        template: "<li>${Name}</li>",
        dataBound: function (e)
        {
            $("#team").find("li").dblclick(function (e)
            {
                e.stopPropagation();
                var node = team.select().data();
                var item = team.dataSource.getByUid(node.uid);

                var newItem = already.dataSource.get(item.id);
                if (newItem) return;
                newItem = { id: item.Id, text: item.Name, type: "Team" };
                already.dataSource.data().push(newItem);
            });
        },
        change: function (e)
        {
            var node = team.select().data();
            var item = team.dataSource.getByUid(node.uid);
            employee.dataSource.read({ TeamId: item.id });
        }
    }).data("kendoListView");

    employee = $("#employee").kendoListView(
    {
        dataSource:
        {
            transport: { read: { url: "/HR/Employee/Read?ResultType=Json&Top=10000"} },
            schema:
            {
                data: "Data",
                model: { id: "Id" },
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        selectable: "single",
        template: "<li>${Name}</li>",
        dataBound: function (e)
        {
            $("#employee").find("li").dblclick(function (e)
            {
                e.stopPropagation();
                var node = employee.select().data();
                var item = employee.dataSource.getByUid(node.uid);

                var newItem = already.dataSource.get(item.id);
                if (newItem) return;
                newItem = { id: item.Id, text: item.Name, type: "User" };
                already.dataSource.data().push(newItem);
            });
        }
    }).data("kendoListView");

    var dataItems = [];
    $.each(data, function (i, n)
    {
        dataItems.push({ id: n.Id, text: n.Text, type: n.Type });
    });
    already = $("#already").kendoListView(
    {
        dataSource:
        {
            data: dataItems
        },
        selectable: "single",
        template: "<li>${text}</li>",
        dataBound: function (e)
        {
            $("#already").find("li").dblclick(function (e)
            {
                e.stopPropagation();
                var node = already.select().data();
                var item = already.dataSource.getByUid(node.uid);
                var dataItems = already.dataSource.data();
                for (var i = 0; i < dataItems.length; i++)
                {
                    if (dataItems[i].id == item.id)
                    {
                        already.dataSource.data().splice(i, 1);
                        return;
                    }
                }
            });
        }
    }).data("kendoListView");

    $(".search").find("button").click(function ()
    {
        employee.dataSource.read({ Name: $(".search").find("input").val() });
        $(".search").find("input").focus();
    });
    $(".search").find("input").keydown(function (e)
    {
        if (e.keyCode == 13)
        {
            employee.dataSource.read({ Name: $(".search").find("input").val() });
            return false;
        }
    });
});


function getData()
{
    var items = [];
    var dataItems = already.dataSource.data();
    $.each(dataItems, function (i, n)
    {
        items.push({ Id: n.id, Text: n.text, Type: n.type });
    });

    var data = items.length == 0 ? null : JSON.stringify(items);
    return data;
}